// pages/login/login.js
import * as FetchAPI from '../../wxapi/api/dispatch.js'
// import * as Utils from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValueUsername: '',
    inputValuePwd: '',
    mobileInfoSystem: '',
    loading: false,
    disabled: false
  },
  //事件
  usernameInput: function (e) {
    this.data.inputValueUsername = e.detail.value
  },
  pwdInput: function (e) {
    this.data.inputValuePwd = e.detail.value
  },
  loginClick: function () {
    let that = this
    that.setData({
      loading: true,
      disabled: true
    })
    let check_res = that.checkLoginParams()
    if (check_res) {
      wx.showLoading({
        title: '登录中...',
        icon: 'loading',
        mask: true
      })
      const toParams = {
        "username": that.data.inputValueUsername,
        "password": that.data.inputValuePwd,
        // "grantType": "password",
        // "clientId": "devops",
        // "clientSecret": "devops"
      }
      FetchAPI.userWeChatLogin(toParams).then(res => {
        if (res.data) {
          let resToken = res.data
          wx.setStorageSync('Authorization', resToken)
          wx.showLoading({
            title: '获取用户信息...',
            icon: 'loading',
            mask: true
          })
          FetchAPI.apiGetUserInfo(resToken).then(resTwo => {
            if (resTwo.data) {
              that.setData({loading: false,disabled: false})
              wx.showToast({title: "登录成功",icon: "success",duration: 1500})
              wx.setStorageSync('userInfo', resTwo.data)
              setTimeout(() => {
                wx.redirectTo({
                  url: '../reportList/reportList'
                })
              }, 1500)
            }
            wx.hideLoading()
          }).catch(errTwo => {
            wx.hideLoading()
            that.setData({loading: false,disabled: false})
            wx.showToast({title: "用户信息获取失败",icon: "none",duration: 1500})
            console.log('error', errTwo)
          })
        }
      }).catch(err => {
        wx.hideLoading()
        that.setData({loading: false,disabled: false})
        wx.showToast({title: "用户或密码错误",icon: "none",duration: 1500})
        console.log('error', err)
      })
      // 获取微信账号登录时的code
      // let resCode = wx.getStorageSync('resCode')
      // if (resCode) {}
    }
  },
  // 账号 和 密码 校检
  checkLoginParams: function () {
    let { inputValueUsername, inputValuePwd } = this.data
    if (inputValueUsername === '') {
      wx.showToast({
        title: "请填写账号",
        icon: "none",
        duration: 1500
      })
      return
    }
    if (inputValuePwd === '') {
      wx.showToast({
        title: "请填写密码",
        icon: "none",
        duration: 1500
      })
      return
    }
    return true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    let latitude, longitude
    // 获取地理位置
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              // 用户已经同意小程序使用位置功能，后续调用 wx.getLocation 接口不会弹窗询问
              wx.getLocation({
                type: 'wgs84', //返回可以用于wx.openLocation的经纬度
                success: function (res) {
                  console.log('getLocation', res)
                  latitude = res.latitude
                  longitude = res.longitude
                  that.data.lats = latitude
                  that.data.lons = longitude
                  //console.log('lat,lon', that.data)
                  let nowLocation = {
                    lats: latitude,
                    lons: longitude
                  }
                  //console.log('nowLocation', nowLocation)
                  wx.setStorageSync('nowLocation', nowLocation)
                }
              })
            }
          })
        }
      }
    })
    wx.getLocation({
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        //console.log('getLocation', res)
        latitude = res.latitude
        longitude = res.longitude
        that.data.lats = latitude
        that.data.lons = longitude
        //console.log('lat,lon', that.data)
        let nowLocation = {
          lats: latitude,
          lons: longitude
        }
        //console.log('nowLocation', nowLocation)
        wx.setStorageSync('nowLocation', nowLocation)
      }
    })
    // 获取设备信息
    let resMobileInfo = wx.getSystemInfoSync()
    //console.log('resMobileInfo', resMobileInfo.system)
    that.data.mobileInfoSystem = resMobileInfo.system
    that.setData({
      inputValueUsername: 'kyk',
      inputValuePwd: 'kyk123456',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})